package cn.stylefeng.guns.modular.single.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.single.Param.PatientParam;
import cn.stylefeng.guns.modular.single.entity.DiseaseType;
import cn.stylefeng.guns.modular.single.entity.Patient;
import cn.stylefeng.guns.modular.single.entity.PatientEscalation;
import cn.stylefeng.guns.modular.single.mapper.PatientDAO;
import cn.stylefeng.guns.modular.single.mapper.PatientEscalationDAO;
import cn.stylefeng.guns.modular.single.service.PatientService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserOrg;
import cn.stylefeng.roses.kernel.sys.modular.user.mapper.SysUserMapper;
import cn.stylefeng.roses.kernel.sys.modular.user.mapper.SysUserOrgMapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class PatientServiceImpl  extends ServiceImpl<PatientDAO, Patient> implements PatientService {
    @Resource
    private PatientEscalationDAO patientEscalationDao;
    @Resource
    private SysUserOrgMapper dao;
    @Resource
    private SysUserMapper userDao;
    @Resource
    private PatientDAO patientDao;
    public PageResult<Patient> findPage(PatientParam patientParam){
        SysUserOrg userOrg =  dao.selectOne(Wrappers.<SysUserOrg>lambdaQuery().eq(SysUserOrg::getUserId, patientParam.getBelongDoctor()));
        LambdaQueryWrapper<Patient> wrapper = Wrappers.<Patient>lambdaQuery()
                .eq(userOrg!=null&& userOrg.getOrgId()!=null,Patient::getBelongOrg, userOrg.getOrgId())
                .in(Patient::getStatus,0,1,4,5,-1);
        Page<Patient> page = (Page)this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(page);
    }
    public  PageResult<Patient> findReportPage(PatientParam patientParam){
        LambdaQueryWrapper<Patient> wrapper = Wrappers.<Patient>lambdaQuery()
                .eq(patientParam.getBelongDoctor()!=null,Patient::getBelongDoctor, patientParam.getBelongDoctor())
                .in(Patient::getStatus,2,3);
        Page<Patient> page = (Page<Patient>)this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(page);
    }

    public  int saveAndReport(JSONObject disease, int status){
        LoginUser user = LoginContext.me().getLoginUser();
        SysUser sysUser = userDao.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserId, user.getUserId()));

        PatientEscalation patientEscalation = new PatientEscalation();
        patientEscalation.setStatus(status);
        patientEscalation.setDiseaseCode(disease.getString("diseaseCode"));
        patientEscalation.setInNo(disease.getString("inNo"));
        patientEscalation.setDiseaseName(disease.getString("diseaseName"));
        patientEscalation.setContent(JSON.toJSONString(disease.getJSONObject("data")));
        patientEscalation.setEscalationName(sysUser.getNickName());
        patientEscalation.setName(disease.getString("name"));
        patientEscalation.setEscalationName(sysUser.getRealName());
        patientEscalation.setOutDate(DateUtil.parse(disease.getString("outDate"),"yyyy-MM-dd HH:mm:ss"));
        Date date =new Date();
        patientEscalation.setCreateDate(date);
        Date escalationDate = null;
/*        if(status == 2){
            escalationDate = date;
        }*/
        patientEscalation.setEscalationDate(escalationDate);
        patientDao.update(null, Wrappers.<Patient>lambdaUpdate()
                .set(Patient::getStatus,status)
                .set(Patient::getEscalationId,user.getUserId())
                .set(Patient::getEscalationName,sysUser.getNickName())
                 .set(Patient::getEscalationDate,escalationDate)
                .eq(Patient::getId,disease.getInteger("id")));
        if(!patientEscalationDao.exists(Wrappers.<PatientEscalation>lambdaQuery().eq(PatientEscalation::getInNo, disease.getString("inNo")))){
            return patientEscalationDao.insert(patientEscalation);
        }else{
            return patientEscalationDao.update(patientEscalation,Wrappers.<PatientEscalation>lambdaUpdate().eq(PatientEscalation::getInNo,disease.getString("inNo")));
        }

    }
}
